package com.lin.lazybone.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lin.lazybone.R;
import com.lin.lazybone.entity.ChatBean;
import com.lin.lazybone.utils.L;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by mr.lin on 2017/11/24.
 */

public class ChatListAdapter extends BaseAdapter
{
    private Context mContext;
    private List<ChatBean> mList;
    private LayoutInflater layoutInflater;

    public ChatListAdapter(Context mContext, List<ChatBean> mList)
    {
        this.mContext = mContext;
        this.mList = mList;
        layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public int getItemViewType(int position)//状态为0时候是左边消息，状态为1时候为右边消息
    {
        return mList.get(position).getType();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
//        ViewHolder viewHolder = null;
//        if (convertView == null)
//        {
//            viewHolder = new ViewHolder();
//            if (getItemViewType(position) == 0)//状态为0时候是左边消息，状态为1时候为右边消息
//            {
//                convertView = layoutInflater.inflate(R.layout.chat_left_item, null);
//                viewHolder.chatlist_touxiang = (CircleImageView) convertView.findViewById(R.id.chatlist_touxiang_left);
//                viewHolder.chatlist_content = (TextView) convertView.findViewById(R.id.chatlist_content_left);
//            }else
//            {
//                convertView = layoutInflater.inflate(R.layout.chat_right_item, null);
//                viewHolder.chatlist_touxiang = (CircleImageView) convertView.findViewById(R.id.chatlist_touxiang_right);
//                viewHolder.chatlist_content = (TextView) convertView.findViewById(R.id.chatlist_content_right);
//            }
//            convertView.setTag(viewHolder);
//        }else
//        {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
        CircleImageView chatlist_touxiang;
        TextView chatlist_content;
        if (getItemViewType(position) == 0)
        {
            convertView = layoutInflater.inflate(R.layout.chat_left_item, null);
            chatlist_touxiang = (CircleImageView) convertView.findViewById(R.id.chatlist_touxiang_left);
            chatlist_content = (TextView) convertView.findViewById(R.id.chatlist_content_left);
        }else
        {
            convertView = layoutInflater.inflate(R.layout.chat_right_item, null);
            chatlist_touxiang = (CircleImageView) convertView.findViewById(R.id.chatlist_touxiang_right);
            chatlist_content = (TextView) convertView.findViewById(R.id.chatlist_content_right);

        }
        ChatBean chatBean = mList.get(position);
        chatlist_touxiang.setImageResource(chatBean.getTouxiang());
        chatlist_content.setText(chatBean.getContent());
        return convertView;
    }

    @Override
    public boolean isEnabled(int position)
    {
        return false;
    }

    class ViewHolder
    {
        private CircleImageView chatlist_touxiang;
        private TextView chatlist_content;
    }
}
